library(tidyr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(zoo)
## Warning: package 'zoo' was built under R version 3.4.4
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(ggplot2)
library(plotly)
## Warning: package 'plotly' was built under R version 3.4.4
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(arcgisbinding)
## *** Please call arc.check_product() to define a desktop license.
arc.check_product()
## product: ArcGIS Pro ( 12.0.1.8933 )
## license: Advanced
# Source hydraulic geometry functions
source("//mvrdfs/egis/Work/Office/Regional/ERDC/EMRRP_Sediment/Methods/FluvialGeomorphr/HydraulicGeometry2.R")
flowline_points <- arc2sp("//mvrdfs/egis/Work/Office/Regional/ERDC/EMRRP_Sediment/Sites2/GalenaBuncombe_05415000/GalenaBuncombe_05415000.gdb/flowline_points")
flowline_pts <- data.frame(flowline_points@data)
# Calculate rolling median
#flowline_pts$Z_smooth <- zoo::rollapply(flowline_pts$Z, width = 100, FUN = median, fill = NA)
# Calculate a loess smoothed z
l_z_5 <- loess(Z ~ POINT_M, data = flowline_pts, span = 0.05)
flowline_pts$Z_smooth <- predict(l_z_5)
# Calculate slope (rise / run)
flowline_pts$slope <- (flowline_pts$Z_smooth - lag(flowline_pts$Z_smooth, 50)) / 50
# Remove na intruduced by the rolling window analysis
flowline_pts <- na.omit(flowline_pts)
# Convert from wide to long format
fp <- gather(flowline_pts[,c("POINT_M","Z","Z_smooth","slope")], 
             key = key, value = value, c(Z, Z_smooth, slope))
p <- ggplot(fp[fp$key != "slope",], 
            aes(x = POINT_M, y = value, color = key)) + 
       geom_point() + 
       scale_color_brewer(palette = "Dark2") + 
       theme_bw() + 
       labs(x = "Distance to Mouth (kilometers)",
            y = "Elevation (feet)")
ggplotly(p)
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`
p2 <- ggplot(fp[fp$key != "Z",], 
             aes(x = POINT_M, y = value, color = key)) + 
       geom_point() + 
       scale_color_brewer(palette = "Dark2") + 
       theme_bw() + 
       labs(x = "Distance to Mouth (kilometers)",
            y = "Elevation (feet)") + 
       facet_grid(key ~ ., scales = "free_y") +
       theme(strip.background = element_blank())
ggplotly(p2)
## We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`